The present invention relates to Java™ virtual machines, and in particular, to string operations in Java™ virtual machines.
Unless otherwise indicated herein, the approaches described in this section are not prior art to the claims in this application and are not admitted to be prior art by inclusion in this section.
The Java™ programming language is a high-level language that may be characterized by all of the following buzzwords: simple, object oriented, distributed, multithreaded, dynamic, architecture neutral, portable, high performance, robust, and secure. Further details regarding each of these buzzwords can be found in “The Java™ Language Environment” by James Gosling and Henry McGilton.
In the Java™ programming language, source code is first written in plain text files ending with the .java extension. These source files are then compiled into .class files by a Java™ compiler such as javac. A .class file does not contain code that is native to a physical processor; it instead contains bytecodes—the machine language of the Java™ Virtual Machine (Java™ VM). A launcher tool (java.exe or other Java™ runtime environment) then runs the application with an instance of the Java™ VM.
The Java™ VM runs the application by converting the Java™ bytecodes into native instructions that are specific to the actual operating system and processor of the computing device. Since the bytecode is designed to be portable, but the Java™ VM is specific to the actual computing device, the Java™ VM may be modified in order to perform a wider variety of tasks yet still remain compliant with the Java™ standard.
Java™ has three core classes for string storage and operations: the String class, the StringBuffer class, and the StringBuilder class. The String class is Java's™ standard way to handle text. Any given String in Java™ is an immutable object, which means its state cannot be changed. A String has an array of characters and information about where in the array the string characters start and end. A character array (in the remainder of this document sometimes abbreviated as “char[capacity]”) is a Java™ object that is located on a heap. It is also, for example, used in String and StringBuilder objects. The character array may be larger than required for the string. Whenever a String must be manipulated, any changes require the creation of a new String (which in almost all cases involves the creation of a new array of characters, and copying of the original array). (Note that in some cases, many implementations of String.substring( ) share the character array with the result string, and just use a different start and offset index; this is possible because strings and their arrays are immutable.) This happens even if the original String's value or intermediate Strings used for the manipulation are not kept.
A StringBuffer, like a String, has an array to hold characters. The StringBuffer, however, is mutable (its state can be altered). Its array of characters is not necessarily completely filled (as opposed to a String, whose array is normally the exact required length for its contents). Thus, it has the capability to add, remove, or change its state without creating a new object (and without the creation of a new array, and array copying). The exception to this is when its array is no longer of suitable length to hold its content. In this case, it is required to create a new array, and copy contents.
The StringBuilder class differs from StringBuffer in that StringBuilder is unsynchronized. When only a single thread at a time will access the object, using a StringBuilder is more efficient than using a StringBuffer.